package com.hw.core.system.service;

import com.hw.core.system.entity.Group;
import com.hw.core.system.repository.GroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true)//全部方法只读事务
@CacheConfig(cacheNames = "groups")
@Service
public class GroupService {
    @Autowired
    private GroupRepository groupRepository;

    @Cacheable(key = "'list'")
    public List<Group> findAll() {
        return groupRepository.findAll();
    }

    @Transactional
    @CacheEvict(allEntries = true)
    public Group add(Group group) {
        return groupRepository.save( group );
    }

    @Cacheable(key = "#id")
    public Group getGroupById(Integer id) {
        return groupRepository.findOne( id );
    }

    @Transactional
    @CacheEvict(allEntries = true)
    public void update(Group group) {
        Group one = groupRepository.findOne( group.getId() );
        one.setName( group.getName() );
        one.setDescription( group.getDescription() );
    }

    @Transactional
    @CacheEvict(allEntries = true)
    public void delete(Integer id) {

        groupRepository.delete( id );


    }
}
